package com.tengyun.middleplatform.trade.tradedata.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.PeekingIterator;
import com.tengyun.data.Result;
import com.tengyun.middleplatform.trade.tradedata.service.INoticeService;
import com.tengyun.middleplatform.trade.tradedata.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: likailun
 * @Description: 消息通知接收
 * @Date: create in 2022/8/29 2:49 PM
 */
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {

    @Resource
    private INoticeService noticeService;
    @PostMapping("/order")
    public Result orderMessage(@RequestBody String message, HttpServletResponse response) {
        try {
            log.info("接收订单mq消息通知:{}", message);
            noticeService.orderNotice(message);
        } catch (Exception be){
            log.error("接收mq消息通知异常，", be);
            log.error(be.getMessage(), be);
            response.setStatus(500);
            try {
                response.getWriter().write(Objects.requireNonNull(JsonUtil.toJson(Result.fail("处理异常"))));
            } catch (IOException e) {
                log.error("接收cmq消息通知异常，", e);
            }
        }
        return Result.success();
    }
}
